﻿using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Localization;
using Orchard.UI.Notify;
using Dyn.News.Models;
using Orchard;
using System.Linq;
using Dyn.News.ViewModels;
using Orchard.Data;
using System.Collections.Generic;
using Dyn.Media.Models;
using Dyn.Media.Settings;
using Dyn.Media.ViewModels;
using Dyn.Media.Services;
using Dyn.News.Services;
using System.Web.Mvc;
namespace Dyn.News.Drivers
{
    [UsedImplicitly]

    public class NewsCategoryMenuWidgetPartDriver : ContentPartDriver<NewsCategoryMenuWidgetPart>
    {
        private readonly INotifier _notifier;
        private const string TemplateName = "Parts/NewsCategoryMenuWidgetPart";
        private readonly IOrchardServices _service;
        private readonly IMediaListService _mediaListService;
        private readonly ICategoryervice _categorySVC;
        private readonly IRepository<NewsContentCategoryMenuWidgetRecord> _categoryMenuWidget;
        private readonly IRepository<NewsCategoryPartRecord> _category;
        public Localizer T { get; set; }

        public NewsCategoryMenuWidgetPartDriver(INotifier notifier, 
            IOrchardServices service,
            IRepository<NewsContentCategoryMenuWidgetRecord> categoryMenuWidget, 
            IMediaListService mediaListService,
            IRepository<NewsCategoryPartRecord> category,
            ICategoryervice categorySVC)
        {
            _category = category;
            _categorySVC = categorySVC;
            _mediaListService = mediaListService;
            _categoryMenuWidget = categoryMenuWidget;
            _service = service;
            _notifier = notifier;
            T = NullLocalizer.Instance;
        }

        protected override DriverResult Display(NewsCategoryMenuWidgetPart part, string displayType, dynamic shapeHelper)
        {
            //Hiển thị menu
            part.CategoryMenus =_categorySVC.GetCategories(part).OrderBy(o => o.DisplayOrder).ToList();
            return ContentShape("Parts_NewsCategoryMenuWidgetPart",
                () => shapeHelper.Parts_NewsCategoryMenuWidgetPart(WidgetVM: part));
        }

        protected override DriverResult Editor(NewsCategoryMenuWidgetPart part, dynamic shapeHelper)
        {
            part.CategoryMenus = _categorySVC.GetCategories(part).OrderBy(o => o.DisplayOrder).ToList();
            return ContentShape("Parts_NewsCategoryMenuWidgetPart",
                    () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: part, Prefix: Prefix));
        }

        protected override DriverResult Editor(NewsCategoryMenuWidgetPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            updater.TryUpdateModel(part, Prefix, null, null);

            var ids = new List<NewsContentCategoryMenuWidgetRecord>();
            var categoryMenus = part.CategoryMenus.Where(w => w.Id != 0);
            foreach (var item in categoryMenus)
            {
                //Lấy ra menu tren giao dien
                var catwg = part.Record.ContentCategoryMenuWidgets.Where(p => p.NewsCategoryPartRecord.Id == item.Id && p.NewsCategoryMenuWidgetPartRecord.Id == part.Record.Id);
                //Nếu có chọn menu
                if (catwg.Count() > 0)
                {
                    //Lấy ra record của ContentMenuWidgets
                    NewsContentCategoryMenuWidgetRecord _content = catwg.FirstOrDefault();
                    _content.DisplayOrder = item.DisplayOrder;
                    _content.IsEnable = true;
                    _categoryMenuWidget.Update(_content);
                    ids.Add(_content);
                }
                else
                {
                    if (item.Id != 0)
                    {
                        NewsContentCategoryMenuWidgetRecord _content = new NewsContentCategoryMenuWidgetRecord()
                        {
                            DisplayOrder = item.DisplayOrder,
                            NewsCategoryPartRecord = _service.ContentManager.Get<NewsCategoryPart>(item.Id).Record,
                            NewsCategoryMenuWidgetPartRecord = part.Record,
                            IsEnable = true,
                        };
                        _categoryMenuWidget.Create(_content);
                        ids.Add(_content);
                    }
                }
            }
            foreach (var item in part.Record.ContentCategoryMenuWidgets)
            {
                var menuwg = ids.Where(p => p.Id == item.Id);
                if (menuwg.Count() == 0)
                {
                    _categoryMenuWidget.Delete(item);
                }
            }

            _notifier.Information(T("Category Widget edited successfully"));
            return Editor(part, shapeHelper);
        }
    }
}